package v2;

/**
 * @Description: 子弹射击
 * @CreateTime: 2024-01-29  17:09
 * @Author: JAMILA
 * @Version: 1.0
 */

public class Shot implements Runnable {
    private int x;//子弹x坐标
    private int y;//子弹y坐标
    private int direct = 0;//子弹方向：0-上 1-右 2-下 3-左
    private int speed = 2;//子弹移动速度
    private boolean isLive = true;//子弹存活状态

    public Shot(int x, int y, int direct, int speed) {
        this.x = x;
        this.y = y;
        this.direct = direct;
        this.speed = speed;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    //射击
    @Override
    public void run() {
        while (true) {
            //休眠 50毫秒
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向来改变x,y坐标
            switch (direct) {
                case 0://上
                    y -= speed;
                    break;
                case 1://右
                    x += speed;
                    break;
                case 2://下
                    y += speed;
                    break;
                case 3://左
                    x -= speed;
                    break;
            }

            //System.out.println("玩家子弹坐标：（" + x + "," + y + "）");

            //当子弹移动到面板的边界时，就应该销毁（把启动的子弹的线程销毁)
            //当子弹击中敌人坦克后，就销毁
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750&&isLive)) {
                //System.out.println("子弹线程退出");
                isLive = false;
                break;
            }
        }


    }
}
